//表格配置
var dataTableconfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/user/queryUser",
		columns : [
				{
					data : "user_id",
					render : function(data, type, full) {
						return '<input type="checkbox" id=\"check_'+data+'\" value="' + data + '" name="items" />';
					}
				}, {
					"data" : "user_id"
				}, {
					"data" : "username"
				}, {
					"data" : "realname"
				}, {
					"data" : "mobile"
				}, {
					"data" : "email"
				}, {
					"data" : "org_name"
				}, {
					"data" : "role_name"
				},{
					"data" : "role_id"
				} ,{
					"data" : "org_id"
				} ,{
					"data" : "password"
				} ,{
					"data" : "is_lock"
				} ],
		columnDefs: [    
		             {    
		            	 "targets": [8,9,10,11], //隐藏第六列，从第0列开始   
						 "visible": false    
					}], 
		"fnDrawCallback": function( oSettings ) {
												//回调函数,调用方式$('#user_table').DataTable().draw();
					},
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",

	};

//初始化表格
var table = $('#user_table').DataTable(dataTableconfig);

$(function() {
	//全选
	$("#checkall").click(function() {
		$('[name=items]:checkbox').prop("checked", this.checked);
	});
	$('[name=items]:checkbox').click(
			function() {
				var $tmp = $('[name=items]:checkbox');
				$('#checkall').prop('checked',
						$tmp.length == $tmp.filter(':checked').length);
			});

	//输出值
	$("#send").click(function() {
		var str = "你选中的是：\r\n";
		$('[name=items]:checkbox:checked').each(function() {
			str += $(this).val() + "\r\n";
		})
		alert(str);
	});
});



var app = angular.module('userApp', []);
//初始化导航栏
var initController = app
		.controller(
				"userController",
				function($scope, $http) {
					$scope.addClick = function(){
						document.getElementById("addForm").reset();
						$('#myModal').modal("toggle");
					}
					$('#myModal').on('hide.bs.modal', function () {
						document.getElementById("addForm").reset(); 
					});
					$http({
						url : '/user/queryRoleList',
						method : 'GET'
					}).success(function(data, header, config, status) {
						// 响应成功
						$scope.roleList = data;
					}).error(function(data, header, config, status) {
						// 处理响应失败
					});
					$http({
						url : '/role/queryOrgList',
						method : 'GET'
					}).success(function(data, header, config, status) {
						// 响应成功
						$scope.orgList = data;
					}).error(function(data, header, config, status) {
						// 处理响应失败
					});
					$scope.add = function(){
						var form = document.getElementById("addForm");
						var username = form.username_add.value;
						var password = form.password_add.value;
						var realname = form.realname.value;
						var mobile = form.mobile.value;
						var email = form.email.value;
						var org_id = form.org_id.value;
						var role_id = form.role_id.value;
						var is_lock = form.is_lock.value;
						$http({
							url : '/user/add',
							params:{username:username, password:password, realname:realname, mobile:mobile ,
									email:email, org_id:org_id, role_id:role_id, is_lock,is_lock},
							method : 'GET'
						}).success(function(data, header, config, status) {
							// 响应成功
							if(data=="1"){
								$('#addSuccessAlert').fadeIn(2000);
								$('#addSuccessAlert').fadeOut(2000, function(){
									$('#myModal').modal('toggle');
								});
								table.draw();
							}else{
								$('#addFailedAlert').fadeIn(2000);
								$('#addFailedAlert').fadeOut(2000);
							}
						}).error(function(data, header, config, status) {
							// 处理响应失败
							$('#addFailedAlert').fadeIn(2000);
							$('#addFailedAlert').fadeOut(2000);
						});
					};
					
					$scope.del_init=function(){
						var menuIds = "";
						$('[name=items]:checkbox:checked').each(function() {
							menuIds += $(this).val() + ";";
						});
						if(menuIds==""){
							$('#SelAlert').html("<strong>请选择要删除的记录！</strong>");
							$('#alertModal').modal("toggle");
						}else{
							$("#myModal3").modal("toggle");
						}
						
					};
					
					$scope.del = function(){
						var userIds = "";
						$('[name=items]:checkbox:checked').each(function() {
							userIds += $(this).val() + ";";
						});
						if(userIds==""){
							alert("请选择要删除的记录！");
						}else{
							$http({
								url : '/user/delete',
								params:{userIds:userIds},
								method : 'GET'
							}).success(function(data, header, config, status) {
								// 响应成功
								if(data==true){
									$('#promptContent').hide();
									$('#del').hide();
									$('#deleteSuccessAlert').fadeIn(2000);
									$('#myModal3').on('hide.bs.modal', function () {
										table.draw();
										$('#del').show();
										$('#deleteSuccessAlert').hide();
										$('#promptContent').show();
									});
								}else{
									$('#promptContent').hide();
									$('#del').hide();
									$('#deleteFailedAlert').fadeIn(2000);
									$('#myModal3').on('hide.bs.modal', function () {
										table.draw();
										$('#del').show();
										$('#deleteFailedAlert').hide();
										$('#promptContent').show();
									});
								}
							}).error(function(data, header, config, status) {
								// 处理响应失败
								$('#promptContent').hide();
								$('#del').hide();
								$('#deleteFailedAlert').fadeIn(2000);
								$('#myModal3').on('hide.bs.modal', function () {
									$('#del').show();
									$('#deleteFailedAlert').hide();
									$('#promptContent').show();
								});
							});
						}
						
					};
					$scope.update_init =function(){
						var userIds = "";
						$('[name=items]:checkbox:checked').each(function() {
							userIds += $(this).val() + ";";
						});
						if(userIds==""){
							$('#SelAlert').html("<strong>请选择要修改的记录！</strong>");
							$('#alertModal').modal("toggle");
						}else if(userIds.split(";").length>2){
							$('#SelAlert').html("<strong>修改的记录数不能大于1.</strong>");
							$('#alertModal').modal("toggle");
						}else{
							var table = document.getElementById("user_table");
							 $("table :checkbox").each(function(key,value){
						            if($(value).prop('checked')){
						            	var selRow =$('#user_table').dataTable().fnGetData(key-1);
						            	$scope.user_id_update = selRow.user_id;
						            	$scope.username_update = selRow.username;
						            	$scope.password_update = selRow.password;
						            	$scope.realname_update = selRow.realname;
						            	$scope.mobile_update = selRow.mobile;
						            	$scope.email_update = selRow.email;
						            	$scope.org_id_update = selRow.org_id;
						            	$scope.role_id_update =selRow.role_id;
						            	$scope.is_lock_update =selRow.is_lock;
						            	$scope.role_name_update = selRow.role_name;
						            	$scope.org_name_update = selRow.org_name;
						            }
							 });
							 $("#myModal2").modal("toggle");
							 document.getElementById("updateForm").reset();
						}
					};
					$scope.update = function(){
						var form = document.getElementById("updateForm");
						var user_id = form.user_id_update.value;
						var username = form.username_update.value;
						var password = form.password_update.value;
						var realname = form.realname_update.value;
						var mobile = form.mobile_update.value;
						var email = form.email_update.value;
						var org_id = form.org_id_update.value;
						var role_id = form.role_id_update.value;
						var is_lock = form.is_lock_update.value;
						$http({
							url : '/user/edit',
							params:{user_id:user_id, username:username, password:password, realname:realname ,
										mobile:mobile, email:email, org_id:org_id, role_id:role_id},
							method : 'GET'
						}).success(function(data, header, config, status) {
							// 响应成功
							if(data=="1"){
								$('#updateSuccessAlert').fadeIn(2000);
								$('#updateSuccessAlert').fadeOut(2000, function(){
									$('#myModal2').modal('toggle');
								});
								table.draw();
							}else{
								$('#updateFailedAlert').fadeIn(2000);
								$('#updateFailedAlert').fadeOut(2000);
							}
						}).error(function(data, header, config, status) {
							// 处理响应失败
							$('#updateFailedAlert').fadeIn(2000);
							$('#updateFailedAlert').fadeOut(2000);
						});
					};
					
					$scope.query = function () {
						var url = '/user/queryUser?test=test';
						var form =document.getElementById("queryForm");  // $('#queryForm');
						if(form.userRealName_query.value!=0){
							url = url + '&userRealName=' + form.userRealName_query.value;
						}
						if(form.org_name_query.value!=0){
							url = url + '&org_name=' + form.org_name_query.value;
						}
						if(form.roleName_query.value!=0){
							url = url + '&roleName=' + form.roleName_query.value;
						}
						if(form.mobile_query.value!=0){
							url = url + '&mobile=' + form.mobile_query.value;
						}
						if(form.email_query.value!=0){
							url = url + '&email=' + form.email_query.value;
						}
						$('#user_table').dataTable().api().destroy();
						dataTableconfig.ajax=url;
						DataTables=$('#user_table').dataTable(dataTableconfig);
					};
				});